class PendinghorsesController < ApplicationController
  # GET /pendinghorses
  # GET /pendinghorses.xml
  def index
    @pendinghorses = Pendinghorse.paginate :per_page => 10, :page => params[:page]
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @pendinghorses }
    end
  end
  
  # GET /pendinghorses/1
  # GET /pendinghorses/1.xml
  def show
    @pendinghorse = Pendinghorse.find(params[:id])
    
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @pendinghorse }
    end
  end
  
  # GET /pendinghorses/new
  # GET /pendinghorses/new.xml
  def new
    @pendinghorse = Pendinghorse.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @pendinghorse }
    end
  end
  
  # GET /pendinghorses/1/edit
  def edit
    @pendinghorse = Pendinghorse.find(params[:id])
  end
  
  # POST /pendinghorses
  # POST /pendinghorses.xml
  def create
    @pendinghorse = Pendinghorse.new(params[:pendinghorse])
    
    respond_to do |format|
      if @pendinghorse.save
        flash[:notice] = 'Pendinghorse was successfully created.'
        format.html { redirect_to(@pendinghorse) }
        format.xml  { render :xml => @pendinghorse, :status => :created, :location => @pendinghorse }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @pendinghorse.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  def aceptar
    if authorize
      @horse = Horse.new
      @pending = Pendinghorse.find(params[:pendinghorse])
      @horse.horse_name = @pending.horse_name
      @horse.horse_sex_id = @pending.horse_sex_id
      @horse.born = @pending.born
      @horse.died = @pending.died
      @horse.mother_horse_id = @pending.mother_horse_id
      @horse.father_horse_id = @pending.father_horse_id
      @horse.family_id = @pending.family_id
      
      respond_to do |format|
        if @horse.save
          flash[:notice] = "#{@horse.horse_name} was successfully created."
          @pending.destroy
          format.html { redirect_to(:controller => "horses", :action => "index") }
          #        format.xml  { render :xml => @pendinghorse, :status => :created, :location => @pendinghorse }
        else
          #        format.html { render :action => "new" }
          #        format.xml  { render :xml => @pendinghorse.errors, :status => :unprocessable_entity }
        end
      end
    end
  end
  
  # PUT /pendinghorses/1
  # PUT /pendinghorses/1.xml
  def update
    @pendinghorse = Pendinghorse.find(params[:id])
    
    respond_to do |format|
      if @pendinghorse.update_attributes(params[:pendinghorse])
        flash[:notice] = 'Pendinghorse was successfully updated.'
        format.html { redirect_to(@pendinghorse) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @pendinghorse.errors, :status => :unprocessable_entity }
      end
    end
  end
  
  # DELETE /pendinghorses/1
  # DELETE /pendinghorses/1.xml
  def destroy
    if authotize
      @pendinghorse = Pendinghorse.find(params[:id])
      @pendinghorse.destroy
      
      respond_to do |format|
        format.html { redirect_to(pendinghorses_url) }
        format.xml  { head :ok }
      end
    end
  end
  protected
  def authorize
    unless session[:user_id]
      flash[:notice] = "<div class='invisible'>Por favor identifiquese<div>"
      redirect_to :controller => 'admin', :action => 'login', :class => 'nyroModal', :rev=>'gal'
    else
      user = User.find(session[:user_id])
      if user.user_tipe_id == 1
        return true        
      else
        return false
      end
    end
  end
end
